package com.ljtxxhwj.myblob.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljtxxhwj.myblob.entity.*;
import com.ljtxxhwj.myblob.entity.ArticleExample;
import com.ljtxxhwj.myblob.mapper.ArticleMapper;
import com.ljtxxhwj.myblob.mapper.ArticleMapper;
import com.ljtxxhwj.myblob.entity.ArticleExample;
import com.ljtxxhwj.myblob.entity.ArticleExample;
import com.ljtxxhwj.myblob.entity.User;
import com.ljtxxhwj.myblob.entity.UserExample;
import com.ljtxxhwj.myblob.mapper.ArticleMapper;
import com.ljtxxhwj.myblob.mapper.ArticleMapper;
import com.ljtxxhwj.myblob.mapper.UserMapper;
import com.ljtxxhwj.myblob.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public User selectUserById(Integer userId) {
        return userMapper.selectUserById(userId);
    }
    @Override
    public User login(User user, HttpSession session) {
        User select = userMapper.select(user);
        if (select!=null){
            session.setAttribute("userId",select.getUserId());
            System.out.println("session = " + session);
            System.out.println("user = " + user);
            return select;
        }
        return null;
    }

    @Override
    public boolean checkCaptcha(String captcha1,HttpSession session) {
        String captcha = (String) session.getAttribute("captcha");
        System.out.println("captcha = " + captcha);
        return captcha.equalsIgnoreCase(captcha1);
    }


    @Override
    public List<UserWithStateName> findByUname(String userName) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserNameLike("%"+userName+"%");
        List<UserWithStateName> userSearchList= userMapper.selectByExample(userExample);
        return userSearchList;
    }

    @Override
    public void backByUid(Integer userId) {

        //恢复用户
        userMapper.backById(userId);
    }

    @Override
    public void closeByUid(Integer userId) {

        //冻结用户
        userMapper.closeById(userId);
    }

    @Override
    public void deleteByUid(Integer userId) {

        //注销用户
        userMapper.deleteById(userId);
    }

    @Override
    public int closeBatch(Integer id) {
        return userMapper.closeBatch(id);
    }

    @Override
    public int deleteBatch(Integer id) {
        return userMapper.deleteBatch(id);
    }

    @Override
    public PageInfo<UserWithStateName> loadWithPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<UserWithStateName>  list = userMapper.loadWithPage();
        PageInfo<UserWithStateName> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void updateUser(Integer userId, String userName, String userIntroduce, String userEmail, String userPhone, String userPassword) {
        System.out.println("userId = " + userId);
        System.out.println("userName = " + userName);
        System.out.println("userIntroduce = " + userIntroduce);
        System.out.println("userEmail = " + userEmail);
        System.out.println("userPhone = " + userPhone);
        System.out.println("userPassword = " + userPassword);

        int result = userMapper.updateUser(userId, userName, userIntroduce, userEmail, userPhone, userPassword);
        if (result == 0) {
            throw new RuntimeException("更新用户信息失败");
        }
    }
    @Override
    public void updateUser(User user) {
        if (user.getUserId() != null) {
            userMapper.updateUserId(user.getUserId(), user.getUserId());
        }
        if (user.getUserName() != null) {
            userMapper.updateUserName(user.getUserId(), user.getUserName());
        }
        if (user.getUserPassword() != null) {
            userMapper.updateUserPassword(user.getUserId(), user.getUserPassword());
        }
        if (user.getUserPic() != null) {
            userMapper.updateUserPic(user.getUserId(), user.getUserPic());
        }
        if (user.getUserIntroduce() != null) {
            userMapper.updateUserIntroduce(user.getUserId(), user.getUserIntroduce());
        }
        if (user.getUserPhone() != null) {
            userMapper.updateUserPhone(user.getUserId(), user.getUserPhone());
        }
        if (user.getUserEmail() != null) {
            userMapper.updateUserEmail(user.getUserId(), user.getUserEmail());
        }
    }
    @Override
    public void updateUserName(Integer userId, String newUserName) {
        userMapper.updateUserName(userId, newUserName);
    }

    @Override
    public void updateUserPassword(Integer userId, String newUserPassword) {
        userMapper.updateUserPassword(userId, newUserPassword);
    }

    @Override
    public void updateUserPic(Integer userId, String newUserPic) {
        userMapper.updateUserPic(userId, newUserPic);
    }

    @Override
    public void updateUserIntroduce(Integer userId, String newUserIntroduce) {
        userMapper.updateUserIntroduce(userId, newUserIntroduce);
    }

    @Override
    public void updateUserPhone(Integer userId, String newUserPhone) {
        userMapper.updateUserPhone(userId, newUserPhone);
    }

    @Override
    public void updateUserEmail(Integer userId, String newUserEmail) {
        userMapper.updateUserEmail(userId, newUserEmail);
    }
}
